﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_UC_SP_TheoNhom : System.Web.UI.UserControl
{
    BUS_Product Data = new BUS_Product();
    BUS_GroupProduct group = new BUS_GroupProduct();
    static DataTable tbGioHang = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["GioHang"] != null)
        {
            tbGioHang = Session["GioHang"] as DataTable;
        }
        else
        {
            tbGioHang.Rows.Clear();
            tbGioHang.Columns.Clear();
            tbGioHang.Columns.Add("idSP", typeof(int));
            tbGioHang.Columns.Add("TenSP", typeof(string));
            tbGioHang.Columns.Add("Gia", typeof(double));
            tbGioHang.Columns.Add("SoLuong", typeof(int));
            tbGioHang.Columns.Add("TongTien", typeof(double), "SoLuong * Gia");
        }
        loadData();
    }
   
    public void loadData()
    {
        DataTable dt = new DataTable();
        ltrTenNSX.Text = TenNhom();
        ltrNhom.Text = TenNhom();
        string id_nhom = Request.QueryString["nhom"];
        dt = Data.Product_GetByTop(0, "GroupProduct_Id like '%" + id_nhom + "%' and [Status] = 1", "[Order]");
        // Starting paging here.
        PagedDataSource pds = new PagedDataSource();
        DataView dv = new DataView(dt);
        pds.DataSource = dv;
        pds.AllowPaging = true;
        // Show number of product in one page.
        pds.PageSize = 4;
        // Specify sum of page.
        int numPage = pds.PageCount;

        int currentPage = 0;
        if (Request.QueryString["page"] != null)
        {
            try
            {

                currentPage = Int32.Parse(Request.QueryString["page"]);
            }
            catch
            {
                Response.Redirect(Request.CurrentExecutionFilePath + "?nhom="+id_nhom);
            }
        }
        else
        {
            currentPage = 1;
        }
        // Because paging always start at 0.
        if (currentPage > 0 && currentPage <= numPage)
        {
            pds.CurrentPageIndex = currentPage - 1;
        }
        else
        {
            Response.Redirect(Request.CurrentExecutionFilePath);
        }
        // Show
        Label1.Text = "Page " + currentPage + " of " + pds.PageCount;


        lnkCurrent.Text = Convert.ToString(currentPage);
        if (currentPage == 1)
        {
            Page.Title += TenNhom();
        }
        else
            Page.Title = TenNhom()+ " - Trang " + currentPage;

        if (!pds.IsFirstPage)
        {
            lnkPre.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage - 1);
            lnkFirst.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom;
            if (currentPage - 1 > 0)
            {
                lnkCurrent_1.Text = Convert.ToString(currentPage - 1);
                lnkCurrent_1.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage - 1);
            }
            else lnkCurrent_1.Visible = false;

            if (currentPage - 1 > 1)
            {
                lnkCurrent_2.Text = Convert.ToString(currentPage - 2);
                lnkCurrent_2.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage - 2);
            }
            else lnkCurrent_2.Visible = false;
            if (currentPage - 1 > 2)
            {
                lnkCurrent_3.Text = Convert.ToString(currentPage - 3);
                lnkCurrent_3.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage - 3);
            }
            else lnkCurrent_3.Visible = false;

        }
        else
        {
            lnkFirst.Visible = false;
            lnkPre.Visible = false;
            lnkCurrent_1.Visible = false;
            lnkCurrent_2.Visible = false;
            lnkCurrent_3.Visible = false;
        }
        if (!pds.IsLastPage)
        {

            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage + 1);
            lnkLast.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(numPage);
            if (currentPage + 1 <= numPage)
            {
                lnkCurrent1.Text = Convert.ToString(currentPage + 1);
                lnkCurrent1.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage + 1);
            }
            else lnkCurrent1.Visible = false;
            if (currentPage + 2 <= numPage)
            {
                lnkCurrent2.Text = Convert.ToString(currentPage + 2);
                lnkCurrent2.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage + 2);
            }
            else lnkCurrent2.Visible = false;
            if (currentPage + 3 <= numPage)
            {
                lnkCurrent3.Text = Convert.ToString(currentPage + 3);
                lnkCurrent3.NavigateUrl = Request.CurrentExecutionFilePath + "?nhom=" + id_nhom + "&page=" + Convert.ToString(currentPage + 3);
            }
            else lnkCurrent3.Visible = false;
        }
        else
        {
            lnkLast.Visible = false;
            lnkNext.Visible = false;
            lnkCurrent1.Visible = false;
            lnkCurrent2.Visible = false;
            lnkCurrent3.Visible = false;
        }

        rptNhaSanXuat.DataSource = pds;
        rptNhaSanXuat.DataBind();
    }


    public string TenNhom()
    {
        string id_nhom = Request.QueryString["nhom"];
        var dt = group.GroupProduct_GetByID(id_nhom);
        string name = "";
        name = dt.Rows[0]["Name"].ToString();
        return name;
    }

    protected void rptProductNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "MuaHang")
        {
            int IdSp = int.Parse(e.CommandArgument.ToString());

            string strTenSP = ((Label)e.Item.FindControl("lblTenSanPham")).Text;
            float flGia = float.Parse(((Label)e.Item.FindControl("lblGia")).Text);
            int intSoLuong = 1;
            foreach (DataRow row in tbGioHang.Rows)
            {//Kiem tr neu mat hang da co roi thi tang so luong len 1
                if ((int)row["idSP"] == IdSp)
                {
                    row["SoLuong"] = (int)row["SoLuong"] + 1;
                    goto GioHang;
                }
            }
            tbGioHang.Rows.Add(IdSp, strTenSP, flGia, intSoLuong);
        GioHang:
            Session["GioHang"] = tbGioHang;


            Response.Redirect(Request.RawUrl);
        }
    }
}